function isValidEmailAddress(emailAddress) 
{
	var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
	return pattern.test(emailAddress);
}

function checkin_datein_dateout()
{
	
}

function autoLoads()
{
	var type_room = $("#type_room").val();
	var number_room = $("#number_of_room").val();
	if(type_room!="")
	{
		// tinh toan ngay den-ngaydi
		var checkIn_day 	= $("#dy_check_in_date").val();
		var checkIn_mo 		= $("#mo_check_in_date").val();
		var checkIn_yr 		= $("#yr_check_in_date").val();
		
		var checkOut_day 	= $("#dy_check_out_date").val();
		var checkOut_mo 	= $("#mo_check_out_date").val();
		var checkOut_yr 	= $("#yr_check_out_date").val();
		
		var xIn= new Date();
		xIn.setFullYear(checkIn_yr,checkIn_mo,checkIn_day);  // ngay den 
		
		var xOut= new Date();
		xOut.setFullYear(checkOut_yr,checkOut_mo,checkOut_day); // ngay di
		
		var ngay = Math.abs(xOut - xIn)/86400000;
		if(ngay<1)
			ngay = 1;
		//alert(ngay/86400000);
		
		var mydepositrequest  = '#hotel_deposit_name_' + type_room;  // phan tram dat truoc 5%
		var mydeposit = $(mydepositrequest).val();
		
		var mydrequest = '#hotel_price_name_' + type_room;
		var max = $(mydrequest).val();
		var mysplit = max.split("*|*");
		var mycurrent_price = mysplit[3]*ngay*number_room;   // gi phong hien tai
		// tinh tien dat coc theo %
		mydeposit = (mycurrent_price/100)*mydeposit;
		
		
		$("#hotel_max").val(mysplit[0]);
		$("#hotel_price_show").val(mycurrent_price);
		$("#hidden_hotel_price").val(mycurrent_price);
		
		$("#hotel_deposit_show").val(mydeposit);
		$("#hidden_hotel_deposit").val(mydeposit);
	}
	else
	{
		// tinh toan ngay den-ngaydi
		var checkIn_day 	= $("#dy_check_in_date").val();
		var checkIn_mo 		= $("#mo_check_in_date").val();
		var checkIn_yr 		= $("#yr_check_in_date").val();
		
		var checkOut_day 	= $("#dy_check_out_date").val();
		var checkOut_mo 	= $("#mo_check_out_date").val();
		var checkOut_yr 	= $("#yr_check_out_date").val();
		
		var xIn= new Date();
		xIn.setFullYear(checkIn_yr,checkIn_mo,checkIn_day);  // ngay den 
		
		var xOut= new Date();
		xOut.setFullYear(checkOut_yr,checkOut_mo,checkOut_day); // ngay di
		
		var ngay = Math.abs(xOut - xIn)/86400000;
		if(ngay<1)
			ngay = 1;
		//alert(ngay/86400000);
		
		
		var mydepositrequest  = '#hotel_deposit_name_1';
		var mydeposit = $(mydepositrequest).val();
		
		var mydrequest = '#hotel_price_name_1';
		var max = $(mydrequest).val();
		var mysplit = max.split("*|*");
		//var mycurrent_price = mysplit[3]*number_room;
		
		var mycurrent_price = mysplit[3]*ngay*number_room;   // gi phong hien tai
		// tinh tien dat coc theo %
		mydeposit = (mycurrent_price/100)*mydeposit;
		
		
		$("#hotel_max").val(mysplit[0]);
		$("#hotel_price_show").val(mycurrent_price);
		$("#hidden_hotel_price").val(mycurrent_price);
		
		$("#hotel_deposit_show").val(mydeposit);
		$("#hidden_hotel_deposit").val(mydeposit);
	}	
}

$(document).ready(function(){		
	//**************************************************************************/
	autoLoads();
	$("#dy_check_in_date, #mo_check_in_date, #yr_check_in_date, #dy_check_out_date, #mo_check_out_date, #yr_check_out_date").change(function(){
		autoLoads();
	});
	
	$("#number_of_room,#type_room").change(function(){
		autoLoads();		
	});
	
	//**************
	$("#submit_book_tour1").click(function(){
		
		var agent_name 		= $("#agent_name").val();
		var agent_email 	= $("#agent_email").val();
		var agent_address 	= $("#agent_address").val();
		var agent_phone 	= $("#agent_phone").val();		
		var agent_message 	= $("#agent_message").val();
		
		var type_room 	= $("#type_room").val();
		
		var checkIn_day 	= $("#dy_check_in_date").val();
		var checkIn_mo 		= $("#mo_check_in_date").val();
		var checkIn_yr 		= $("#yr_check_in_date").val();
		
		var checkOut_day 	= $("#dy_check_out_date").val();
		var checkOut_mo 	= $("#mo_check_out_date").val();
		var checkOut_yr 	= $("#yr_check_out_date").val();
		
		//alert(checkIn_yr+' - '+checkIn_mo+' - '+checkIn_day);
		
		var tour_captcha	 	= $("#captcha").val();
		var compare1 = "";
		var compare2 = "";
		var compare3 = "";
		
		var xIn= new Date();
		xIn.setFullYear(checkIn_yr,checkIn_mo,checkIn_day);
		
		var xOut= new Date();
		xOut.setFullYear(checkOut_yr,checkOut_mo,checkOut_day);
		
		var today = new Date();
		
		
		if (xIn>today)
		{
			compare1 = 'true';			
		}
		if (xOut>today)
		{
			compare2 = 'true';			
		}
		if (xIn<xOut)
		{
			compare3 = 'true';			
		}
		//alert(xIn+' - '+xOut+' - '+today);
		var tamp = "";
		if(agent_name=="")
		{
			$("#contact_error_name").removeClass('contact_error');
			$("#contact_error_name").addClass('contact_error_show');
			tamp += "ten";
		}
		else
		{
			$("#contact_error_name").removeClass('contact_error_show');
			$("#contact_error_name").addClass('contact_error');
		}
		/*---------------------------------------------------------*/
		if(agent_email=="" || !isValidEmailAddress(agent_email))
		{
			$("#contact_error_email").removeClass('contact_error');
			$("#contact_error_email").addClass('contact_error_show');
			tamp += "ten";
		}
		else
		{
			$("#contact_error_email").removeClass('contact_error_show');
			$("#contact_error_email").addClass('contact_error');
		}
		/*---------------------------------------------------------*/
		if(agent_address=="")
		{
			$("#contact_error_address").removeClass('contact_error');
			$("#contact_error_address").addClass('contact_error_show');
			tamp += "ten";
		}
		else
		{
			$("#contact_error_address").removeClass('contact_error_show');
			$("#contact_error_address").addClass('contact_error');
		}
		/*---------------------------------------------------------*/
		if(agent_phone=="")
		{
			$("#contact_error_phone").removeClass('contact_error');
			$("#contact_error_phone").addClass('contact_error_show');
			tamp += "ten";
		}
		else
		{
			$("#contact_error_phone").removeClass('contact_error_show');
			$("#contact_error_phone").addClass('contact_error');
		}
		
		/*---------------------------------------------------------*/
		if(agent_message=="")
		{
			$("#contact_error_message").removeClass('contact_error');
			$("#contact_error_message").addClass('contact_error_show');
			tamp += "ten";
		}
		else
		{
			$("#contact_error_message").removeClass('contact_error_show');
			$("#contact_error_message").addClass('contact_error');
		}
		/*---------------------------------------------------------*/
		/* Company infomation */
		/*---------------------------------------------------------*/
		
		if(compare1=="")
		{
			$("#contact_error_check_in").removeClass('contact_error');
			$("#contact_error_check_in").addClass('contact_error_show');
			tamp += "ten";
		}
		else
		{
			$("#contact_error_check_in").removeClass('contact_error_show');
			$("#contact_error_check_in").addClass('contact_error');
		}
		/*---------------------------------------------------------*/
		if(compare2=="")
		{
			$("#contact_error_check_out").removeClass('contact_error');
			$("#contact_error_check_out").addClass('contact_error_show');
			tamp += "ten";
		}
		else
		{
			$("#contact_error_check_out").removeClass('contact_error_show');
			$("#contact_error_check_out").addClass('contact_error');
		}
		/*---------------------------------------------------------*/
		if(compare3=="")
		{
			alert("Please check again Check in date and Check out date!");
			tamp += "ten";
		}
		/*---------------------------------------------------------*/
		if(type_room=="")
		{
			$("#contact_error_type_room").removeClass('contact_error');
			$("#contact_error_type_room").addClass('contact_error_show');
			tamp += "ten";
		}
		else
		{
			$("#contact_error_type_room").removeClass('contact_error_show');
			$("#contact_error_type_room").addClass('contact_error');
		}
		/*---------------------------------------------------------*/
		if(tour_captcha=="")
		{
			$("#contact_error_captcha").removeClass('contact_error');
			$("#contact_error_captcha").addClass('contact_error_show');
			tamp += "ten";
		}
		else
		{
			$("#contact_error_captcha").removeClass('contact_error_show');
			$("#contact_error_captcha").addClass('contact_error');
		}
		/*---------------------------------------------------------*/
		
		
		if(tamp!="")
			return false;
		else return true;
		/*else
		{
			return true;
			
			var url =  $("#myprocess_url").val();	
			var show =  $("#myprocess_show_result").val();	
						
			var agentString = agent_name+'*|*'+agent_email+'*|*'+agent_address+'*|*'+agent_phone+'*|*'+agent_message;
			var companyString = company_name+'*|*'+company_email+'*|*'+company_address+'*|*'+company_phone+'*|*'+company_fax+'*|*'+company_message;
			url = url + "?action=travel-agent&agent="+agentString+'&company='+companyString;			
			dbagentsend(url,show);
			
		}*/		
	});	
});